package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.Info.AllStatePageInfo;
import com.tracesystem.controller.Info.AllTypePageInfo;
import com.tracesystem.controller.form.*;
import com.tracesystem.model.IssueState;
import com.tracesystem.dao.IssueStateDao;
import com.tracesystem.model.IssueType;
import com.tracesystem.service.IssueStateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.IssueStateException;
import com.tracesystem.util.exception.IssueTypeException;
import com.tracesystem.util.exception.code.IssueStateExceptionCode;
import com.tracesystem.util.exception.code.IssueTypeExceptionCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssueStateServiceImpl extends ServiceImpl<IssueStateDao, IssueState> implements IssueStateService {

    /**
     * 缺陷状态实现层对象
     */
    @Resource
    private IssueStateDao issueStateDao;

    /**
     * 缺陷状态业务层对象
     */
    @Resource
    private IssueStateService issueStateService;

    //修改缺陷状态分类
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> updateStateDesc(RenewalStateDescForm renewalStateDescForm) {
        IssueState issueState = issueStateService.getOne(//查询类型是否存在
                new QueryWrapper<IssueState>().eq("STATE_CODE", renewalStateDescForm.getStateCode()));
        if(null==issueState) {
            throw new IssueStateException(IssueStateExceptionCode.IssueState_NOTEXIST_ERROR);
        }else {
            if(issueState.getStateDesc().equals(renewalStateDescForm.getStateDesc())){
                throw new IssueStateException(IssueStateExceptionCode.IssueState_EXIST_ERROR);
            }else {
                boolean b = issueStateService.saveOrUpdate(WoniuCopyUtil.copyOne(renewalStateDescForm, IssueState.class));
                if(!b) return Result.getInstance().setMsg("缺陷状态不存在,已经为您新增该缺陷状态");
                return Result.getInstance().setMsg("修改缺陷状态成功");
            }
        }
    }

    //修改缺陷状态
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> updateModuleState(RenewalStateModuleStateForm renewalStateModuleStateForm) {
        IssueState issueState = issueStateService.getOne(//查询类型是否存在
                new QueryWrapper<IssueState>().eq("STATE_CODE", renewalStateModuleStateForm.getStateCode()));
        if (issueState.getModuleState().equals("有效")){
            issueState.setModuleState("禁用");
        }else {
            issueState.setModuleState("有效");
        }
        int i = issueStateDao.updateById(WoniuCopyUtil.copyOne(issueState, IssueState.class));
        if(i>0) return Result.getInstance().setMsg("修改缺陷状态成功");
        return Result.getInstance().setCode(50002).setMsg("修改缺陷状态失败");
    }

    //根据缺陷状态名模糊查询缺陷状态分类
    @Override
    public Result<AllStatePageInfo> findPageStateListByStateDesc(LikeStateListForm likeStateListForm) {
        QueryWrapper<IssueState> wrapper = new QueryWrapper<IssueState>().orderByDesc("STATE_CODE");
        if(likeStateListForm.getStateDesc() != null && !likeStateListForm.getStateDesc().trim().equals(""))
            wrapper.like("STATE_DESC",likeStateListForm.getStateDesc());

        Page<IssueState> wa = page(new Page<>(likeStateListForm.getPageIndex(),likeStateListForm.getPageSize()),wrapper);
        List<AllStatePageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllStatePageInfo.class);

        Page<AllStatePageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    //获取所有缺陷状态
    @Override
    public Result<AllStatePageInfo> getAllStateList(FindAllStateListForm findAllStateListForm) {
        QueryWrapper<IssueState> wrapper = new QueryWrapper<IssueState>().orderByDesc("STATE_CODE");

        Page<IssueState> wa = page(new Page<>(findAllStateListForm.getPageIndex(),findAllStateListForm.getPageSize()),wrapper);
        List<AllStatePageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllStatePageInfo.class);

        Page<AllStatePageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    //添加缺陷状态
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> insertState(AddStateForm addStateForm) {
        IssueState issueState = issueStateService.getOne(//查询类型是否存在
                new QueryWrapper<IssueState>().eq("STATE_DESC", addStateForm.getStateDesc()));
        //存在则抛出已存在异常
        if(null!=issueState) throw new IssueStateException(IssueStateExceptionCode.IssueState_EXIST_ERROR);
//        addStateForm.setModuleState("0");//默认值为0
        issueStateService.save(WoniuCopyUtil.copyOne(addStateForm,IssueState.class));
        return Result.getInstance().setMsg("新增缺陷状态成功");
    }

    //删除缺陷状态
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectState(RemoveStateForm removeStateForm) {
        boolean b = issueStateService.removeById(removeStateForm.getStateCode());
        if(!b) throw new IssueStateException(IssueStateExceptionCode.IssueState_NOTEXIST_ERROR);
        return Result.getInstance().setMsg("删除缺陷状态成功");
    }

    //批量删除缺陷状态
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectStateList(RemoveStateListForm removeStateListForm) {
        List<Integer> StateCodes = removeStateListForm.getStateCodes();
        System.out.println(StateCodes);
        System.out.println(StateCodes.size());
        for (Integer StateCode : StateCodes) {
            boolean b = issueStateService.removeById(StateCode);
            if(!b) throw new IssueStateException(IssueStateExceptionCode.IssueState_NOTEXIST_ERROR);
            continue;
        }
        return Result.getInstance().setMsg("删除缺陷状态成功");
    }
}
